/**
 * class name: idle,
 * copyright: maximus zhang,
 * License: MIT-LICENSE,
 * contact: maximus.zhang@gmail.com,
 * required-js: http://jsease.googlecode.com/svn/trunk/core.js,
 * update: http://jsease.googlecode.com/svn/trunk/idle.js
 *
 */
function idle(){/*{data,cfg,fn}*/
    var A = arguments,
        _ = this;
    _ = ext( _, ext( {
            id: 'idle',
            elem: '',
            url: "idle.php",
            data: '',
            idle: 'idle',
            step: 29,
            cfg: [
                [0, 60, 29, 'live'],
                [61, 300, 49, 'away1'],
                [301, 600, 69, 'away5'],
                [601, 900, 89, 'away10'],
                [901, 1800, 109, 'idle'],
                [0, 3600, 29, 'hide'],
                [0, 3600, 29, 'busy']
                ],
            fn: function(){}
        }, A[0] )
    );

    /* _.data = ext( { idle: 4, user: '', module: 'Idle', mdlid: 0, locref: '' }, A[0] );
    _.cfg = ext(
        idle:[ { ttl: 60, step: 29 }, { ttl: 300, step: 49 }, { ttl: 600, step: 69 }, { ttl: 900, step: 89 } ],
        offline: {},
        dfn: ,
        step: 29 }, A[1] );
    _.fn = ext({ idle: function(a){;} }, A[2]); */

    _.uitype = 'idle';

    _.last = _._1st = now(2);

    _.timer = (_._1st/1000).int10()%_.step;
    _.bound = false;

    _.reset = function(){
        _.last = now(2);
    };

    _.getIdle = function(t){
        for (var i = 0, L = _.cfg.length; i<L; i++) {
            if ( t>=_.cfg[i][0] && t<=_.cfg[i][1] ) {
                if ( _.idle == _.cfg[i][3] ) {
                    return 0;
                } else {

                    _.idle = _.cfg[i][3];
                    _.step = _.cfg[i][2];

                    return (_.idle);
                }
            }
        }
        return 0;
    };

    _.exec = function(){
        _.data = ext( _.data, arguments[0] );
        var idle = aja$({ url: _.url, data: _.data }, {
            finish : function(a) {
                var v = duc(a);

                if ( v.length>0 ) {
                    if ( _.fn.hasOwnProperty(v) ) {
                        _.fn[v]();
                    } else {
                        try {
                            _.fn.idle(v);
                        } catch (e) {
                            ex(e);
                        }
                    }
                }
            },
            fail : function (a) {
                var v = duc(a);
                ex(v);
            }
        } );
    };

    _.init = function() {
        _.elem = $(_.elem) || me.body;

        if ( $(_.id) ) {
            if ( $(_.id).className == 'idle' ) {
                _.$ = $(_.id);
            } else {
                _.id += 'idle';
                _.$ = elm('div');
            }
        } else {
            _.$ = elm('div');
        }

        _.$.attr('id', _.id).setClass('idle').appendTo(_.elem);

        if ( !_.bound ) {
            bind(me, 'keydown', _.reset);
            bind(me, 'mousemove', _.reset);
            _.bound = true;
        }

        return _.go();
    };

    _.beep = function() {
        var tt = now(2);
        _.data.idle = _.getIdle( parseInt( ( tt-_.last )/1000, 10 ) );

        if ( _.data.idle!==0 || ( parseInt( ( tt-_._1st )/1000, 10 ) % _.step ) == _.timer ) {
            _.exec();
        }
    return _;
    };

    _.go = function(){
        _.run = setInterval(_.beep, 1000);
    return _;
    };

    _.remove = function(){
        unbind(me, 'keydown', _.reset);
        unbind(me, 'mousemove', _.reset);
        for ( x in _ ){
            delete _[x];
        }
        _ = null;
    };
}